$().ready(function() {

	$('.summernote').summernote({
		height : '220px',
		lang : 'zh-CN',
		callbacks: {
            onImageUpload: function(files, editor, $editable) {
                sendFile(files);
            }
        }
	});
	var content = $("#description").val();
	$('#description_cn').summernote('code', content);
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save(1);
	}
});
function save(status) {
	var description_cn = $("#description_cn").summernote('code');
	var formData = new FormData(document.querySelector("form")); 
	var title = $("#title").val();
	formData.set("description",description_cn);
	$.ajax({
		type : "POST",
		url : "/shop/system/goods/save",
		data : formData,// 你的formid
		async : false,
		cache : false,
        contentType : false,
        processData : false,
        dataType : "json",
		success : function(r) {
			if (r.code == 0) {
				parent.layer.msg(r.msg);
				window.location.reload()
			} else {
				parent.layer.alert(r.msg)
			}
		}
	});
}

function deleteImage(goodsId,sortOrder) {
	$.ajax({
		type : "POST",
		url : "/shop/system/goods/delete_image",
		data : JSON.stringify({"goodsId":goodsId,"sortOrder":sortOrder}),// 你的formid
		contentType:"application/json",
        dataType : "json",
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(r) {
			if (r.code == 0) {
				parent.layer.msg(r.msg);
				window.location.reload()
			} else {
				parent.layer.alert(r.msg)
			}
		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			title : "required",
			sku : "required",
			subject : "required",
			thumbnailImage : "required",
			generalUserPrice : "required",
			platinaUserPrice : "required",
			vipUserPrice : "required",
			agent1Price : "required",
			agent2Price : "required",
			agent3Price : "required",
			agent4Price : "required",
			agent5Price : "required",
			amount : "required"
		},
		messages : {
			title : "请填写商品标题",
			sku : "请填写库存单位",
			subject : "请填写商品摘要",
			thumbnailImage : "请选择商品小图",
			generalUserPrice : "请填写普通会员价",
			platinaUserPrice : "请填写白金会员价",
			vipUserPrice : "请填写VIP会员价",
			agent1Price : "请填写代理商价",
			agent2Price : "请填写初级合伙人价",
			agent3Price : "请填写中级合伙人价",
			agent4Price : "请填写普高级合伙人价",
			agent5Price : "请填写普联合创始人价",
			amount : "请填写库存数"
		}
	});
}

function returnList() {
	var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
	parent.layer.close(index);
}